package com.project.biz.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.biz.CmsProductBiz;
import com.project.biz.CmsProductEvaBiz;
import com.project.entity.CmsProductEva;
import com.project.mapper.CmsProductEvaMapper;
import com.project.pojo.dto.ProductEvaDTO;
import com.project.pojo.model.GetProductEvaModel;
import com.project.pojo.model.ProductOrderEvaluateModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description: 商品评价业务层接口实现类
 * @Author: zhuchi
 * @CreateDate: 2020/11/6 20:45
 * @Version: 1.0
 */
@Slf4j
@Service
public class CmsProductEvaEvaImpl extends ServiceImpl<CmsProductEvaMapper, CmsProductEva> implements CmsProductEvaBiz {

    /**
     * 商品业务层接口类
     */
    @Autowired
    private CmsProductBiz productBiz;

    /**
     * 添加商品评价
     * @param productOrderEvaluateModel
     * @param productId 商品ID
     * @param productOrderId 订单ID
     * @param orderItemId 订单详情ID
     * @param userId  用户ID
     * @return
     */
    public int addProductEva(ProductOrderEvaluateModel productOrderEvaluateModel, Long productId, Long productOrderId, Long orderItemId, Long userId){
        int result = 0;
        CmsProductEva productEva = new CmsProductEva();
        BeanUtils.copyProperties(productOrderEvaluateModel, productEva);
        productEva.setProductId(productId);
        productEva.setProductOrderId(productOrderId);
        productEva.setOrderItemId(orderItemId);
        productEva.setCreateTime(new Date());
        productEva.setCreateUser(userId);
        productEva.setStatus(1);
        result = this.baseMapper.insert(productEva);
        if (result > 0){
            //增加商品评论数量
            int productAddEvaNum = this.productBiz.addProductEvaNum(productId);
            log.info("商品[{}]增加评论数量执行状态:{}", productId, productAddEvaNum);
        }

        return result;
    }

    /**
     * 分页查询商品评论
     * @param getProductEvaModel
     * @return
     */
    public IPage<ProductEvaDTO> getProductEva(GetProductEvaModel getProductEvaModel){
        Page<ProductEvaDTO> page = new Page<>(getProductEvaModel.getPageNo(), getProductEvaModel.getPageSize());
        IPage<ProductEvaDTO> iPage = this.baseMapper.getProductEva(page, getProductEvaModel.getProductId());
        return iPage;
    }

}
